package com.source.action;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.DefaultActionSupport;

import com.opensymphony.xwork2.ActionContext;
import java.util.ResourceBundle;

public class FileUploadAction extends DefaultActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Map<String, Object> session;
	private File uploadFile;
	private String uploadFileFileName;
	private String uploadFileContentType;
	private long fileSize;
	private ServletContext sc;
	private String rutaLoad;
	private String rutaDocumento;
	private String nombreDocumento;
	
	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public long getFileSize() {
		return fileSize;
	}

	public void setFileSize(long fileSize) {
		this.fileSize = fileSize;
	}

	public String getUploadFileFileName() {
		return uploadFileFileName;
	}

	public void setUploadFileFileName(String uploadFileFileName) {
		this.uploadFileFileName = uploadFileFileName;
	}

	public File getUploadFile() {
		return uploadFile;
	}

	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}

	public String getUploadFileContentType() {
		return uploadFileContentType;
	}

	public void setUploadFileContentType(String uploadFileContentType) {
		this.uploadFileContentType = uploadFileContentType;
	}
	
	public String getRutaLoad() {
		return rutaLoad;
	}

	public void setRutaLoad(String rutaLoad) {
		this.rutaLoad = rutaLoad;
	}

	public String getRutaDocumento() {
		return rutaDocumento;
	}

	public void setRutaDocumento(String rutaDocumento) {
		this.rutaDocumento = rutaDocumento;
	}	
	
	public String getNombreDocumento() {
		return nombreDocumento;
	}

	public void setNombreDocumento(String nombreDocumento) {
		this.nombreDocumento = nombreDocumento;
	}

	public void cargaMenu() {
		session = ActionContext.getContext().getSession();
	}	

	@Override
	public String execute() throws Exception {
		cargaMenu();
                 ResourceBundle rbundle = ResourceBundle.getBundle("connection");
		File fileToCreate = new File( rbundle.getString("pathLoad").toString(),
				this.uploadFileFileName);
		FileUtils.copyFile(this.uploadFile, fileToCreate);
                HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
                //HttpServletResponse response = ServletActionContext	.getResponse();
		//HttpServletRequest request = ServletActionContext.getRequest();
		rutaLoad = rbundle.getString("pathLoad").toString() + this.uploadFileFileName;
		rutaDocumento = rbundle.getString("pathServerLoad").toString() + this.uploadFileFileName;
		nombreDocumento = this.uploadFileFileName;
		request.setAttribute("rutaDocumento", rutaDocumento);
		request.setAttribute("nombreDocumento", nombreDocumento);
		session.put("rutaLoad", rutaLoad);
		session.put("rutaDocumentoUpload", rutaDocumento);
		session.put("nombreDocumentoUpload", this.uploadFileFileName );
		session.put("modificado", "si");
		return "page02";
	}

//	public Properties getProperties(){
//
//		Properties p = new Properties();
//		try {
//			sc = ServletActionContext.getServletContext();
//			p.load(sc.getResourceAsStream("/WEB-INF/archivos.properties"));
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return p;
//	}
	
}
